#coding:utf-8
'''
@author:        laohu
@date:          2021.07.14
@brief:         资产退回弹出框
@note:          继承自Dialog
'''


from UIComponents.MapLabel import MapLabel
from UserManageDao import UserManageDao
from AssetManageService import AssetManageService
from Infrastructure.AssetStatus import AssetStatus
from ElectronicAssetDAO import ElectronicAssetDAO
from typing import Optional
from PySide6.QtWidgets import QAbstractItemView, QComboBox, QDialog, QDialogButtonBox, QGridLayout, QHBoxLayout, QHeaderView, QLineEdit, QPushButton, QTableWidget, QTableWidgetItem, QWidget
from PySide6.QtCore import Qt
from UIComponents.Dialog.toast import QToast


class ReturnAssetDialog(QDialog):
    def __init__(self, parent: Optional[QWidget] = None, f: Qt.WindowFlags = Qt.Dialog) -> None:
        super().__init__(parent, f)
        self.resize(800, 500)
        self.setWindowTitle("资产退回")
        self.glayout = QGridLayout(self)
        self.hlayout = QHBoxLayout()
        self.assetNumberLineEdit = QLineEdit()
        self.assetNumberLineEdit.setPlaceholderText("资产编号")
        self.hlayout.addWidget(self.assetNumberLineEdit)
        self.buttonAdd = QPushButton("添加")
        self.buttonAdd.clicked.connect(self.buttonAddClicked)
        self.hlayout.addWidget(self.buttonAdd)
        self.glayout.addLayout(self.hlayout, 0, 0)
        self.returnAssetsTable = QTableWidget()
        self.returnAssetsTable.setSelectionMode(QAbstractItemView.ExtendedSelection)  # 设置只可以单选，可以使用ExtendedSelection进行多选
        self.returnAssetsTable.setSelectionBehavior(QAbstractItemView.SelectRows)  # 设置 不可选择单个单元格，只可选择一行。
        self.returnAssetsTable.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.returnAssetsTable.setColumnCount(13)
        self.returnAssetsTable.setHorizontalHeaderLabels(
            ['资产编号','设备编号','设备类别','品牌','型号','使用类型','产地','用途','生产日期','使用位置','使用人','备注','操作'])#设置表头文字
        self.glayout.addWidget(self.returnAssetsTable, 1, 0)
        self.buttons = QDialogButtonBox()
        self.buttons.addButton("确认", QDialogButtonBox.AcceptRole)
        self.buttons.addButton("取消", QDialogButtonBox.RejectRole)
        self.buttons.accepted.connect(self.accept)
        self.buttons.rejected.connect(self.reject)
        self.glayout.addWidget(self.buttons, 2, 0)
        self.setWindowModality(Qt.ApplicationModal)

    def buttonAddClicked(self):
        assetNumber = self.assetNumberLineEdit.text()
        for i in range(0, self.returnAssetsTable.rowCount()):
            if assetNumber == self.returnAssetsTable.cellWidget(i, 0).text():
                toast = QToast(self, "资产编号已存在于待退回列表", 0, 0, "")
                toast.toast()
                return
        args = []
        args.append({"asset_ID": assetNumber, "status": AssetStatus.Allotted})
        args.append({})
        assets = AssetManageService.get_user_device(args)
        if len(assets) is 0:
            toast = QToast(self, "无对应可退回资产", 0, 0, "")
            toast.toast()
            return
        asset = assets[0]
        row = self.returnAssetsTable.rowCount()
        self.returnAssetsTable.insertRow(row)
        deviceInfo = MapLabel(asset[0].asset_ID, asset[0])
        self.returnAssetsTable.setCellWidget(row, 0, deviceInfo)
        self.returnAssetsTable.setItem(row, 1, QTableWidgetItem(asset[0].device_ID))
        self.returnAssetsTable.setItem(row, 2, QTableWidgetItem(asset[0].device_category))
        self.returnAssetsTable.setItem(row, 3, QTableWidgetItem(asset[0].brand))
        self.returnAssetsTable.setItem(row, 4, QTableWidgetItem(asset[0].device_model))
        self.returnAssetsTable.setItem(row, 5, QTableWidgetItem(asset[0].using_type))
        self.returnAssetsTable.setItem(row, 6, QTableWidgetItem(asset[0].origin))
        self.returnAssetsTable.setItem(row, 7, QTableWidgetItem(asset[0].application))
        self.returnAssetsTable.setItem(row, 8, QTableWidgetItem(asset[0].production_date.strftime("%Y\%m\%d")))
        self.returnAssetsTable.setItem(row, 9, QTableWidgetItem(asset[0].using_locate))
        userInfo = MapLabel(asset[1].name, asset[1])
        self.returnAssetsTable.setCellWidget(row, 10, userInfo)
        self.returnAssetsTable.setItem(row, 11, QTableWidgetItem(asset[0].remarks))
        buttonDelete = QPushButton("×")
        buttonDelete.clicked.connect(self.buttonDeleteClicked)
        self.returnAssetsTable.setCellWidget(row, 12, buttonDelete)
        self.assetNumberLineEdit.setText("")

    def buttonDeleteClicked(self):
        self.returnAssetsTable.removeRow(self.returnAssetsTable.currentRow())